import Vue from 'vue'
import VueRouter from 'vue-router'
import User from '../views/User'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import CorrectAnswer from '../views/CorrectAnswer.vue'
import Tubiaotest from '../views/tubiaotest.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    component: User,
    children: [{
        path: '/',
        name: 'Home',
        component: Home
      },
      {
        path: '/manage',
        name: 'Manage',
        component: () => import('../views/Manage')
      },
      {
        path: '/choose',
        name: 'Choose',
        component: () => import('../views/Choose')
      },
      {
        path: '/detail',
        name: 'Detail',
        component: () => import('../views/Detail.vue'),
        children: [{
            path: 'statistic/:QID',
            name: 'Statistic',
            component: () => import('../views/Statistic.vue')
          },
          {
            path: 'publish/:QID',
            name: 'Publish',
            component: () => import('../views/Publish.vue')
          },
          {
            path: 'surveyPage/:QID',
            name: 'SurveyPage',
            component: () => import('../views/SurveyPage.vue')
          },
        ]
      }
    ]
  },
  {
    path: '/answer/:QID64',
    name: 'Answer',
    props: true,
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( /* webpackChunkName: "about" */ '../views/Answer.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/correctAnswer/:QID64',
    name: 'CorrectAnswer',
    props: true,
    component: CorrectAnswer
  },
  {
    path: '/tubiaotest',
    name: 'Tubiaotest',
    component: Tubiaotest
  },
  {
    path: '/voteResult/:QID64',
    name: 'VoteResult',
    props: true,
    component: () => import('../views/VoteResult')
  },
  {
    path: '/Result/:QID64/:TYPE',
    name: 'Result',
    props: true,
    component: () => import('../views/Result')
  },
  {
    path: '/myMap',
    name: 'MyMap',
    component: () => import('../views/MyMap')
  },
  {
    path: '/location',
    name: 'location',
    component: () => import('../components/Editor/QeditorLocation')
  },
  {
    path: '/location01',
    name: 'location01',
    component: () => import('../components/Item/QitemLocation')
  }
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  let isLogin = router.app.$options.store.state.isLogin
  let limited = to.name === 'SurveyPage' || to.name === 'Manage' || to.name === 'Detail' || to.name === 'Statistic' || to.name === 'Publish'
  console.log(isLogin, limited)
  if (!isLogin && limited) {
    alert('请先登录')
    next('Login')
  }
  next()
})

export default router